extern "C" { 
    #include "apue.h" 
}   
#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <dirent.h>

/*
    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);

    int creat(const char *pathname, mode_t mode);

    int openat(int dirfd, const char *pathname, int flags);
    int openat(int dirfd, const char *pathname, int flags, mode_t mode);

    
    DIR *opendir(const char *name);
    DIR *fdopendir(int fd);
    int dirfd(DIR *dirp);

*/

int main(int argc ,char** argv)
{    
    int dir_fd;
    int file_fd;
    // 方式1,使用open打开dir,返回的文件描述为openat的dirfd
    // 方式2,使用opendir,返回值用dirfd 转换为fd

    dir_fd=open("../",O_RDONLY);        //------this line is diff
    if(dir_fd<0)
    {
        err_quit("open ../ dir err");
    }
    file_fd=openat(dir_fd,"./3-3-1/Makefile",O_RDONLY);
    if(file_fd<0)
    {
        err_quit("open Makefile err");
    }

    unsigned char buf[100]={0};
    read(file_fd,buf,99);
    printf("read by open Makefile for 99 byte :\n:%s\n",buf);

    DIR* dirfd_pt=opendir("../");           //------this line is diff
    if(dirfd_pt==NULL)
    {
        err_quit("opendir ../ dir err");
    }
    dir_fd=dirfd(dirfd_pt);              //------this line is diff
    if(dir_fd<0)
    {
        err_quit("dirfd convert err");
    }
    file_fd=openat(dir_fd,"./3-3-1/Makefile",O_RDONLY);
    if(file_fd<0)
    {
        err_quit("open Makefile err");
    }

    unsigned char buf2[100]={0};
    read(file_fd,buf2,99);
    printf("read by opendir  Makefile for 99 byte :\n:%s\n",buf2);


    // creat a file and overwride
    open("./me.txt.o", O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU);


}

// read by open Makefile for 99 byte :
// :SRCS = $(wildcard ./*.cpp)
// #SRCS += $(wildcard ./*.c)
// #OBJS := $(patsubst %.c, %.o, $(SRCS))
// OBJS 
// read by opendir  Makefile for 99 byte :
// :SRCS = $(wildcard ./*.cpp)
// #SRCS += $(wildcard ./*.c)
// #OBJS := $(patsubst %.c, %.o, $(SRCS))
// OBJS 